
#include "Sliderdialog.h"
#include "ui_Sliderdialog.h"

SliderDialog::SliderDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SliderDialog)
{
    setAttribute(Qt::WA_DeleteOnClose);
    ui->setupUi(this);
    Init();
    ConnectAll();
}

SliderDialog::~SliderDialog()
{
    delete ui;
}

void SliderDialog::Init()
{
    setStyleSheet(R"(
        QWidget#centralWidget {
            background-color: rgba(54, 54, 54, 1);
            border-radius: 10px;
            border: 0px;
        }
    )");
    this->setAttribute(Qt::WA_TranslucentBackground); // 背景透明
    this->setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint); // 无边框
}

void SliderDialog::ConnectAll()
{
    connect(ui->slider, SIGNAL(sliderMoved(int)), this, SLOT(DoSliderValueChanged(int)));
    connect(ui->slider, SIGNAL(sliderReleased()), this, SLOT(DoSliderValueChanged()));
}

void SliderDialog::SetLabelSuffix(QString suffix)
{
    mLabelSuffix = suffix;
}

void SliderDialog::SetSliderMaxValue(int value)
{
    ui->slider->setMaximum(value);
}

void SliderDialog::SetSliderMinValue(int value)
{
    ui->slider->setMinimum(value);
}

void SliderDialog::SetSliderStep(int step)
{
    ui->slider->setSingleStep(step);
}

void SliderDialog::SetValueConversion(int rate)
{
    mConversionRate = rate;
}

void SliderDialog::DoSliderValueChanged(int value)
{
    emit SliderValueChanged(value);
    UpdateLabel(value);
}

void SliderDialog::DoSliderValueChanged()
{
    DoSliderValueChanged(ui->slider->value());
}

void SliderDialog::UpdateLabel(int value)
{
    if (mConversionRate > 1) {
        ui->label->setText(QString::asprintf("%0.2f", static_cast<float>(value) / mConversionRate) + mLabelSuffix);
        return;
    }
    ui->label->setText(QString::asprintf("%d", value) + mLabelSuffix);
}

void SliderDialog::SetValue(int value)
{
    ui->slider->setValue(value);
    UpdateLabel(value);
}
